package Offer.offer;

/**
 * 打印1到最大的n位数
 * @author leiwenkang
 * @date 2020/4/6
 */
public class Test12打印1到最大的n位数 {
    public static void main(String[] args) {
        print1ToN(2);
    }

    public static void print1ToN(int n) {
        if (n < 1) {
            return;
        }
        int[] array = new int[n];
        //先初始化为0
        for (int i = 0; i < array.length; i++) {
            array[i] = 0;
        }
        while (add(array)) {
            print(array);
        }
    }

    //模拟加法
    private static boolean add(int[] array) {
        int takeOver = 0;
        for (int i = array.length - 1; i >= 0; i--) {
            int sum = array[i] + takeOver;
            if (i == array.length - 1) {
                sum++;
            }

            if (sum >= 10) {
                sum -= 10;
                takeOver = 1;
                array[i] = sum;
            } else {
                takeOver = 0;
                array[i] = sum;
                break;
            }
        }
        if (takeOver == 1) {
            return false;
        }
        return true;
    }

    private static void print(int[] array) {
        if (array == null || array.length == 0) {
            return;
        }

        boolean startPrint = false;
        for (int i = 0; i < array.length; i++) {
            if (array[i] != 0) {
                startPrint = true;
            }

            if (startPrint) {
                System.out.print(array[i]);
            }
        }
        System.out.print(" ");
    }
}
